<?php
//require_once 'XML_Serializable.php';

class DBElement
//extends XML_Serializable
{
	var $DBElementFields = array ('DBElementFields', 'table_name', 'primary_key_field', 'primary_key_value', 'className', 'connection' );

	/**
	 * The table to which this class belongs
	 *
	 * @var String
	 */
	var $table_name;
	/**
	 * The field of the primary key
	 *
	 * @var String
	 */
	var $primary_key_field;
	/**
	 * The value of the primary key
	 *
	 * @var integer
	 */
	var $primary_key_value;

	/**
	 * The connection to use when making a mysql_query call
	 *
	 * @var link_identifier MySQL connection identifier
	 */
	var $connection;

	/**
	 * Used for updating a field on this DBElement
	 *
	 * @param string $field_name
	 * @param unknown_type $field_value
	 */
	function update($field_name, $field_value) {
		$field_name = mysql_real_escape_string ( $field_name );
		if ($field_value == NULL || strtolower($field_value) == "null") {
			//$field_value is null, set the value to NULL
			$field_value = mysql_real_escape_string ( $field_value );
			$sql = "UPDATE $this->table_name SET $field_name = NULL WHERE $this->primary_key_field='$this->primary_key_value';";
		} else if($field_value == "NOW()"){
			$field_value = mysql_real_escape_string ( $field_value );
			$sql = "UPDATE $this->table_name SET $field_name = NOW() WHERE $this->primary_key_field='$this->primary_key_value';";
		}
		else {
			$field_value = mysql_real_escape_string ( $field_value );
			$sql = "UPDATE $this->table_name SET $field_name = '$field_value' WHERE $this->primary_key_field='$this->primary_key_value';";
		}
		mysql_query ( $sql, $this->connection );
	}

	/**
	 * Used for inserting a row as a subelement (foreign key object) of this element.
	 * Will only make use of the values that have been set.
	 *
	 * @param DBElement $classObject
	 * @return int The new id of the inserted object
	 */
	function insert(&$classObject) {
		/* @var $classObject DBElement */
		$table = $classObject->table_name;

		//get the field for which this objects primary key is the classObject's foreign key
		$foreign_primary_key_field = $this->primary_key_field;
		//set the value of the foreign key to the value of this objects primary key
		$classObject->$foreign_primary_key_field = $this->primary_key_value;

		$fields = "";
		$values = "";
		foreach ( $classObject as $field => $value ) {
			if (in_array ( $field, $this->DBElementFields )) {
				continue;
			}
			//make sure the value has been set, it's not null, and it's not an empty string
			if (isset ( $value ) && $value != NULL && $value != "") {
				$fields .= $field . ", ";
				$values .= "'" . mysql_real_escape_string ( $value ) . "', ";
			}
		}

		//remove the trailing comma and space
		$fields = mysql_real_escape_string ( substr ( $fields, 0, - 2 ) );
		$values = substr ( $values, 0, - 2 );

		//get each field from the class
		$sql = "INSERT INTO $table ($fields) VALUES ($values);";
		mysql_query ( $sql, $this->connection ); //insert the row


		//set the primary_key_value
		$classObject->primary_key_value = mysql_insert_id ( $this->connection );
		//set the actual field value that is the primary key field
		$pkf = $classObject->primary_key_field;
		$classObject->$pkf = $classObject->primary_key_value;

		return mysql_insert_id ( $this->connection );
	}

	/**
	 * Inserts this element in the database.
	 * Requires that the primary_key_field has not been set.
	 * Be sure to use the public fields during instantation, not the setters
	 * @return int The new primary_key_value
	 */
	function insertMe() {
		if (isset ( $this->primary_key_value )) {
			print ( "The primary_key_field cannot be set before inserting this element." );
		} else {
			$table = $this->table_name;
			$fields = "";
			$values = "";
			foreach ( $this as $field => $value ) {
				if (in_array ( $field, $this->DBElementFields )) {
					continue;
				}

				//make sure the value has been set, it's not null, and it's not an empty string
				if (isset ( $value ) && $value != NULL && $value != "") {
					$fields .= $field . ", ";
					$values .= "'" . mysql_real_escape_string ( $value ) . "', ";
				}
			}
				
			//remove the trailing comma and space
			$fields = mysql_real_escape_string ( substr ( $fields, 0, - 2 ) );
			$values = substr ( $values, 0, - 2 );
				
			//get each field from the class
			$sql = "INSERT INTO $table ($fields) VALUES ($values);";
			mysql_query ( $sql, $this->connection ); //insert the row
				

			$this->primary_key_value = mysql_insert_id ( $this->connection );
			//set the actual field that is the primary key field
			$pkf = $this->primary_key_field;
			$this->$pkf = $this->primary_key_value;
			return $this->primary_key_value;
		}
	}

	/**
	 * Deletes this item from the database
	 * @return The primary key value of this object
	 */
	function deleteMeFromDatabase() {
		$sql = "DELETE FROM $this->table_name WHERE $this->primary_key_field = '$this->primary_key_value'";
		mysql_query($sql, $this->connection);
		return $this->primary_key_value;
	}

}

?>